package com.brtbeacon.demo.lot.state;

import android.os.Handler;
import android.view.View;

import androidx.fragment.app.Fragment;

import com.brtbeacon.demo.lot.LotMapActivity;
import com.brtbeacon.demo.lot.R;
import com.brtbeacon.demo.lot.fragment.ParkingRouteControlFragment;
import com.brtbeacon.map.map3d.BRTMapView;
import com.brtbeacon.map.map3d.entity.BRTPoi;
import com.brtbeacon.map.map3d.entity.BRTPoint;
import com.brtbeacon.map.map3d.route.BRTRouteResult;

public class ParkingRouteState extends ParkingState implements View.OnClickListener {

    private ParkingRouteControlFragment parkingRouteControlFragment;
    private Handler handler = new Handler();

    @Override
    public void enter(LotMapActivity activity) {
        super.enter(activity);
        this.activity.setTopFragment(null);
        this.activity.setBottomFragment(getBottomFragment());
    }

    @Override
    public void exit() {
        if (this.activity != null) {
            this.activity.setTopFragment(null);
            this.activity.setBottomFragment(null);
        }
        super.exit();
    }

    private ParkingRouteControlFragment getBottomFragment() {
        if (parkingRouteControlFragment == null) {
            parkingRouteControlFragment = ParkingRouteControlFragment.newInstance();
            parkingRouteControlFragment.setState(this);
        }
        return parkingRouteControlFragment;
    }

    @Override
    public void onFragmentViewCreated(Fragment fragment, View view) {
        super.onFragmentViewCreated(fragment, view);
        BRTPoi poi = activity.getParkingPoi();
        if (poi != null) {
            parkingRouteControlFragment.tvInfo.setText(poi.getName());
        }
        parkingRouteControlFragment.btnOK.setOnClickListener(this);
        parkingRouteControlFragment.tvChange.setOnClickListener(this);
        parkingRouteControlFragment.btnOK.setEnabled(true);
        parkingRouteControlFragment.tvChange.setEnabled(true);
    }

    public void onClickAtPoint(BRTMapView mapView, BRTPoint point) {
        //  TODO... DEBUG CODE!!!!!!
        //this.activity.setLocation(BRTConvert.toLocalPoint(point));
        //this.activity.requestRoute();
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_ok: {
                //  TODO... 设置停车点；
                BRTRouteResult routeResult = activity.getRouteResult();
                if (routeResult != null) {
                    activity.speak("开始导航");
                    activity.setState(ParkingState.PARKING_STATE_NAVIGATION);
                    /*
                    parkingRouteControlFragment.btnOK.setEnabled(false);
                    parkingRouteControlFragment.tvChange.setEnabled(false);
                    handler.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            activity.setState(ParkingState.PARKING_STATE_NAVIGATION);
                        }
                    }, 1500);
                    */
                }
                break;
            }

            case R.id.tv_change: {
                activity.setState(ParkingState.PARKING_STATE_TARGET);
                break;
            }
        }
    }
}
